package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;

import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {

   //远程注入
@Reference
private RoleService roleService;
    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult = roleService.pageQuery(queryPageBean);
        return pageResult;
    }

    //按照用户Id查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Role role =roleService.findById(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,role);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //根据角色Id查询权限Id
    @RequestMapping("/findPermissionIdByRoleId")
    public Result findPermissionIdByRoleId(Integer id){
        try{
            List<Integer> permissions=roleService.findPermissionIdByRoleId(id);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,permissions);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    //根据角色Id查询菜单Id
    @RequestMapping("/findMenuIdByRoleId")
    public Result findMenuIdByRoleId(Integer id){
        try{
            List<Integer> menus=roleService.findMenuIdByRoleId(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,menus);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    //新增用户
    @RequestMapping("/add")
    public Result add(@RequestBody Role role,Integer [] permissions,Integer [] menus){
        try{
            roleService.add(role,permissions,menus);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
        return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
    }

    //删除
    @RequestMapping("delete")
    public Result delete(Integer id){
        //得到id 根据id进行删除
        try{
            roleService.delete(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    //编辑角色
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer [] permissions,Integer [] menus){
        try{
            roleService.edit(role,permissions,menus);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_ROLE_SUCCESSS);
    }


}
